"use strict";

const Op = require("sequelize").Op;
const path = require("path");

module.exports = (appInfo) => {
  const config = (exports = {});

  config.qiniu = {
    accessKey: "umk74BuuIujmdgZZ0BWBEPJnWjxxvgctUFvP7ou1",
    secretKey: "dm3mOv1IgRK84aLqN1wA7Ug4pM-Mj4kZuR1d5YR4",
    bucket: "wechat-chef",
    cdnDomain: "st0h4a5kx.hn-bkt.clouddn.com",
    uploadMaxSize: 1024 * 1024 * 10, // 10MB
  };
  config.rundir = process.cwd() + "/run";
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1707394383972_7725";
  config.logger = {
    dir: path.join(process.cwd(), "logs"),
  };
  config.sms = {
    LoginTemplateId: "templateId",
    AppKey: "204832550",
    AppSecret: "8ndzfnUdbhuDUGSO7GWz4W2q6WlEtC5B",
    AppCode: "2dfeadc46f0e49c7b9cbc3979f7be115",
  };
  config.cluster = {
    // 禁用 Agent 进程的调试端口
    inspectPort: false,
    // 确保 Worker 进程也不启用调试
    workers: process.env.NODE_ENV === "production" ? 2 : 1,
    // 可选：设置日志级别，减少调试信息干扰
    loggerLevel: "INFO",
  };
  // 配置限流中间件选项
  config.rateLimit = {
    windowMs: 1000, // 时间窗口为60秒
    maxRequests: 2, // 默认每分钟最多100次请求
    whitelist: ["127.0.0.1"], // 本地IP白名单
    pathRules: {

    },
  };
  // 中间件配置
  config.middleware = ["rateLimit", "errorHandler", "responseHandler"];
  // 安全配置
  config.security = {
    csrf: {
      enable: false,
    },
    domainWhiteList: ["http://localhost:3000"], // 允许跨域的域名
  };

  // 跨域配置
  config.cors = {
    origin: "*",
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS",
  };
  config.redis = {
    client: {
      port: 6379, // Redis port
      host: "39.105.149.126", // Redis host
      password: "zzq199905",
      db: 0,
    },
    pool: {
      min: 2, // 最小空闲连接数
      max: 10, // 最大连接数
      idle: 10000, // 空闲超过10秒的连接将被回收
    },
  };
  config.proxy = true;
  // 数据库配置
  config.sequelize = {
    dialect: "mysql",
    host: "39.105.149.126",
    port: 3306,
    database: "chef_recruitment",
    username: "root",
    password: "zzq199905",
    timezone: "+08:00",
    define: {
      timestamps: true,
      underscored: true,
      freezeTableName: true,
    },
    sync: false, // 禁用自动同步表结构
    alter: false, // 禁用自动修改表结构
    logging: false, // 关闭 SQL 日志输出
  };

  // JWT配置
  config.jwt = {
    secret: "your-jwt-secret-key",
    enable: true,
    ignore: ["/api/v1/admin/users/login"], // 不需要验证的路由
  };

  // 文件上传配置
  config.multipart = {
    mode: "file",
    whitelist: [
      ".jpg",
      ".jpeg",
      ".png",
      ".gif",
      ".webp",
      ".mp4",
      ".mp3",
      ".xlsx",
      ".xls", // 直接添加到 whitelist
    ],
    // 移除 fileExtensions
    // 添加更多配置以支持中文文件名
    fieldNameSize: 100,
    fieldSize: "10mb",
    fields: 10,
    files: 10,
    fileMimeTypes: [
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      "application/vnd.ms-excel",
      "application/octet-stream", // 添加这个通用的 MIME 类型
    ],
  };

  // 静态文件配置
  config.static = {
    prefix: "/public/",
    static_prefix: "http://localhost:7001",
    dir: process.cwd() + "/public",
    dynamic: true,
    preload: false,
    maxAge: 31536000,
    buffer: true,
  };

  // 错误处理配置
  config.onerror = {
    all(err, ctx) {
      // 在此处定义针对所有响应类型的错误处理方法
      const body = {
        code: err.status || 500,
        message: err.message,
        data: null,
      };
      ctx.status = err.status || 500;
      ctx.set("Content-Type", "application/json");
      ctx.body = JSON.stringify(body);
      console.log("Error response:", body);
    },
  };

  // 参数校验配置
  config.validate = {
    convert: true,
    widelyUndefined: true,
  };

  config.weapp = {
    appId: "wx244954dda7673351", // 小程序 appId
    appSecret: "9e7589a2dc60b49ec9aa7801deb7baa4", // 小程序 appSecret
    // 订阅消息模板ID
    templates: {
      applyNotify: "FhrYrlpaNoylj_3UuemtYblKma9JJI_6hJxrTwUDPEs", // 申请提醒通知
      auditResultNotify: "TmTXchkaM6Cc63xNEzPuB3Q-Vgmnn3PggJXLT1NjlEw", // 审核结果通知
      verifyResultNotify: "Qjg4pGhn-iQfXdvmhXi3u4pgCOzsysYuO7YDJMYajBE", // 核验结果通知
    },
  };

  return {
    ...config,
  };
};
